<?php
declare (strict_types = 1);

namespace App\Logic;

use App\Event\AdministratorLogsEvent;
use App\Models\Tags as TagsModels;
use Illuminate\Support\Facades\DB;

class Tags
{
    public function select()
    {
        return TagsModels::where('tag_status', '=', 1)->orderBy('id','desc')->get();
    }

    public function count(array $where = []): int
    {
        $map[] = ['delete_time', '=', 0];
        if (!empty($where)) {
            if (!empty($where['tag_name'])) $map[] = ['tag_name', 'like',  '%'.$where['tag_name'].'%'];
        }
        return TagsModels::where($map)->count();
    }

    public function list(int $start, int $limit = 10, array $where = [])
    {
        $map[] = ['delete_time', '=', 0];
        if (!empty($where)) {
            if (!empty($where['tag_name'])) $map[] = ['tag_name', 'like',  '%'.$where['tag_name'].'%'];
        }
        return TagsModels::where($map)->orderBy('id','desc')->offset($start)->limit($limit)->get();
    }

    public function add(array $data)
    {
        $model = new TagsModels();
        foreach ($data as $k => $v) {
            $model->$k = $v;
        }
        if ($model->save()) {
            event(new AdministratorLogsEvent("添加标签"));
            return true;
        }
        return false;
    }

    public function update(int $id, array $data)
    {
        $model = TagsModels::find($id);
        foreach ($data as $k => $v) {
            $model->$k = $v;
        }
        if ($model->save()) {
            event(new AdministratorLogsEvent("修改标签"));
            return true;
        }
        return false;
    }

    public function status(int $id)
    {
        $model = TagsModels::find($id);
        if(!empty($model)){
            if ($model->tag_status == 1){
                $model->tag_status = 2;
            }else{
                $model->tag_status = 1;
            }
            if ($model->save()){
                event(new AdministratorLogsEvent("修改标签状态"));
                return true;
            }
        }
        return false;
    }

    public function delete(int $id)
    {
        $model = TagsModels::find($id);
        DB::beginTransaction();
        try {
            $model->articles()->detach();
            $model->delete();
            DB::commit();
            event(new AdministratorLogsEvent("删除标签"));
            return true;
        } catch (\Exception $e) {
            DB::rollBack();
            return false;
        }
    }

    public function find(int $id)
    {
        return TagsModels::find($id);
    }

    public function has(int $id)
    {
        $model = TagsModels::find($id);
        if (empty($model)) return false;
        return true;
    }

    public function checkName(string $name, int $id=0)
    {
        $model = TagsModels::where('tag_name',$name)->where('id','<>',$id)->first();
        if (empty($model)) return true;
        return false;
    }

    public function countArticlesByTagId(int $id)
    {
        $tag = TagsModels::find($id);
        $articles = $tag->articles();
        return $articles->count();
    }
}
